package com.qq.bean;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class User implements InitializingBean, DisposableBean , BeanNameAware, BeanClassLoaderAware, BeanFactoryAware {


    private String username;


    private String password;

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<?> clazz = Class.forName("com.qq.bean.User");
        Object instance = clazz.getDeclaredConstructor().newInstance();
        Method setUsername = clazz.getMethod("setUsername", String.class);
        Method setPassword = clazz.getMethod("setPassword", String.class);
        setUsername.invoke(instance, "张三");
        setPassword.invoke(instance, "123456");
        System.out.println( instance);

    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public User() {
        System.out.println("1 开始实例化，User 对象");
    }

    @Value("小白")
    public void setUsername(String username) {
        System.out.println("2.1 开始设置属性，username" +  username);
        this.username = username;
    }
    public void setPassword(String password) {
        System.out.println("2.2 开始设置属性，password");
        this.password = password;
    }

    public void initBean(){
        System.out.println("6 初始化方法");
    }

    public void destroyBean(){
        System.out.println("9 销毁方法");
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("5 InitializingBean接口的afterPropertiesSet⽅法执⾏");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("8 DisposableBean接口的destroy⽅法执⾏");
    }

    @Override
    public void setBeanName(String s) {

        System.out.println("3.1 BeanName ：" +  s);
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("3.2 BeanClassLoader ：" +  classLoader);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("3.3 BeanFactory ：" +  beanFactory);
    }

}
